﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SusuCMS.Web.Paging;
using System.Web.Handlers;
using System.Web.Routing;
using SusuCMS.Caching;

namespace SusuCMS.Web.Areas.Admin.Controllers
{
    public class PageController : AdminControllerBase
    {
        private void UpdateHomePageCache(string url)
        {
            if (url.ToLower() == "index")
            {
                CmsDataCache.UpdateOnlineHomePages();
            }
        }

        public ActionResult Index()
        {
            var list = PageRepository.FindAll()
                    .Where(i => i.PageTypeId == CurrentPageTypeId);

            return View(list);
        }

        public ActionResult Search(string keywords)
        {
            var list = PageRepository.FindAll()
                .Where(i => i.PageTypeId == CurrentPageTypeId)
                .Where(i => i.Title.Contains(keywords));

            return View("Index", list);
        }

        public ActionResult Create()
        {
            return View(new Page
            {
                PageTypeId = CurrentPageTypeId,
                WebsiteId = CurrentWebsiteId
            });
        }

        [HttpPost]
        public ActionResult Create(Page model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    model.CreateTime = DateTime.Now;
                    model.PublishTime = DateTime.Now;
                    PageRepository.Add(model);
                    UnitOfWork.Save();

                    UpdateHomePageCache(model.Url);

                    TempData["Message"] = "Create Page Successfully";

                    return RedirectToAction("Index");
                }
                catch
                {
                    TempData["ErrorMessage"] = "Create Page Failed";
                }
            }

            return View(model);
        }

        #region Edit
        public ActionResult Edit(int id)
        {
            var page = PageRepository.FindAll().FirstOrDefault(i => i.Id == id);
            if (page == null)
            {
                return HttpNotFound();
            }

            return View(page);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var page = PageRepository.FindAll().FirstOrDefault(i => i.Id == id);
            UpdateModel(page);

            if (ModelState.IsValid)
            {
                try
                {
                    page.PublishTime = DateTime.Now;
                    UnitOfWork.Save();

                    UpdateHomePageCache(page.Url);

                    TempData["Message"] = "Edit Page Successfully";

                    return RedirectToIndex();
                }
                catch
                {
                    TempData["ErrorMessage"] = "Edit Page Failed";
                }
            }

            return View(page);
        }
        #endregion

        #region Delete

        [HttpPost]
        public ActionResult Delete(int id)
        {
            var page = PageRepository.FindAll().FirstOrDefault(i => i.Id == id);
            if (page == null)
            {
                TempData["ErrorMessage"] = "Page Not Exist";
            }
            else
            {
                try
                {
                    var url = page.Url;
                    PageRepository.Remove(page);
                    UnitOfWork.Save();

                    UpdateHomePageCache(url);

                    TempData["Message"] = "Delete Page Successfully";

                    return RedirectToIndex();
                }
                catch
                {
                    TempData["ErrorMessage"] = "Delete Page Failed";
                }
            }

            return RedirectToIndex();
        }
        #endregion

        #region ToggleOnline
        public ActionResult ToggleOnline(int id)
        {
            var page = PageRepository.FindAll().FirstOrDefault(i => i.Id == id);
            if (page == null)
            {
                TempData["ErrorMessage"] = "Page Not Exist";
            }
            else
            {
                try
                {
                    page.IsPublished = !page.IsPublished;
                    UnitOfWork.Save();

                    UpdateHomePageCache(page.Url);

                    TempData["Message"] = "Toggle Page Successfully";

                    return RedirectToIndex();
                }
                catch
                {
                    TempData["ErrorMessage"] = "Toggle Page Failed";
                }
            }

            return RedirectToIndex();
        }

        #endregion


    }
}
